package com.tianma315.core.wx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.tianma315.core.code_flag.domain.CodeFlagDO;
import com.tianma315.core.code_flag.service.CodeFlagService;
import com.tianma315.core.codes.domain.CodesDO;
import com.tianma315.core.codes.service.CodesService;
import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.common.exception.MessageException;
import com.tianma315.core.common.service.ConfigService;
import com.tianma315.core.common.utils.DateUtils;
import com.tianma315.core.product.domain.ProductDO;
import com.tianma315.core.product.service.ProductService;
import com.tianma315.core.report.service.ReportImgService;
import com.tianma315.core.report.service.ReportService;
import com.tianma315.core.report.vo.ReportApiVO;
import com.tianma315.core.scanrecord.domain.ScanRecordDO;
import com.tianma315.core.scanrecord.service.ScanRecordService;
import com.tianma315.core.trace.domain.TraceOutDO;
import com.tianma315.core.trace.domain.TraceabilityFileDO;
import com.tianma315.core.trace.service.TraceOutFieldService;
import com.tianma315.core.trace.service.TraceOutService;
import com.tianma315.core.trace.service.TraceabilityFileService;
import com.tianma315.core.trace.vo.TraceOutAttrFieldVO;
import com.tianma315.core.tymenu.domain.TyMenuVO;
import com.tianma315.core.tymenu.service.TyMenuService;
import com.tianma315.core.wx.dao.WxUserInfoDao;
import com.tianma315.core.wx.domain.WxUserInfoDO;
import com.tianma315.core.wx.domain.WxUserInfoVO;
import com.tianma315.core.wx.service.WxUserInfoService;





import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;


/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-08-23 14:59:05 | auv</small>
 */
@Service
public class WxUserInfoServiceImpl extends CoreServiceImpl<WxUserInfoDao, WxUserInfoDO> implements WxUserInfoService {
	@Autowired
	private ConfigService configService;

	@Autowired
	private WxUserInfoDao wxUserInfoDao;

	@Autowired
	private ReportService reportService;

	@Autowired
	private ReportImgService reportImgService;

	@Autowired
	private ProductService productService;

	@Autowired
	private CodesService codesService;

	@Autowired
	private TraceOutFieldService traceOutFieldService;

	@Autowired
	private TraceOutService traceOutService;

	@Autowired
	private TraceabilityFileService traceabilityFileService;

	@Autowired
	private TyMenuService tyMenuService;

	@Autowired
	private ScanRecordService scanRecordService;

	@Autowired
	private CodeFlagService codeFlagService;



	@Override
	public List<WxUserInfoDO> queryWXUserInfoByIds(List idList) {
		return wxUserInfoDao.queryWXUserInfoByIds(idList);
	}

	@Override
	public WxUserInfoDO getWxUserInfoByOpenId(String openId) {
		EntityWrapper<WxUserInfoDO> wrapper = new EntityWrapper<>();
		wrapper.eq("open_id",openId);
		WxUserInfoDO wxUserInfoDO = selectOne(wrapper);
		return wxUserInfoDO;
	}

	@Override
	public JSONObject getData(String code,String openId) {
		JSONObject result = new JSONObject();

		//根据防伪码找到信息
		CodesDO codes = codesService.getCodesBySmallCode(code);
		if (codes == null){
			throw new MessageException("防伪码不存在");
		}

		Integer serial = Integer.parseInt(codes.getSequence());
		//根据序号查出出库记录
		TraceOutDO traceOutDO = traceOutService.getTraceOutDOBySerial(serial);
		if (traceOutDO == null){
//			result.put("code",10000);
//			result.put("msg","没有找到对应出库记录");

			throw new MessageException("没有找到对应出库记录");
		}

		//获取批次记录值
		List<TraceOutAttrFieldVO> traceOutFieldDOS = traceOutFieldService.getTraceOutAttrFieldByTraceOutId(traceOutDO.getTraceOutId());

		//获取溯源档案
		TraceabilityFileDO traceabilityFileDO = traceabilityFileService.selectById(traceOutDO.getTraceabilityFileId());
		if (traceabilityFileDO == null){
//			result.put("code",10001);
//			result.put("msg","没有找到溯源档案");
			throw new MessageException("没有找到溯源档案");
//			return result;
		}

		//获取产品信息
		ProductDO productDO = productService.getProductById(traceOutDO.getProductId());
		if (productDO == null){
//			result.put("code",10002);
//			result.put("msg","没有找到产品");
			throw new MessageException("没有找到产品");
//			return result;
		}

		//获取报告信息
		ReportApiVO reportApiVO = reportService.getReportApiVOById(traceOutDO.getReportId());
		if (reportApiVO == null){
//			result.put("code",10003);
//			result.put("msg","没有找到报告");
			throw new MessageException("没有找到报告");
//			return result;
		}

		//根据档案id查找整个档案流程
		List<TyMenuVO> tyMenuVOS = tyMenuService.getTyMenuByTraceabilityFileId(traceabilityFileDO.getTraceabilityFileId());
		if (tyMenuVOS.isEmpty()){
//			result.put("code",10004);
//			result.put("msg","没找到档案流程");
			throw new MessageException("没找到档案流程");
//			return result;
		}


		result.put("code",200);
		result.put("product",productDO);
		result.put("report",reportApiVO);
		result.put("traceOutFieldDOS",traceOutFieldDOS);
		result.put("produceCenter",tyMenuVOS);
		result.put("batch",traceOutDO.getBatch());


		//插入查找记录
		ScanRecordDO scanRecord = scanRecordService.getScanRecordByCode(code);
		if (scanRecord == null){
			ScanRecordDO scanRecordDO = new ScanRecordDO();
			scanRecordDO.setOpenId(openId);
			scanRecordDO.setCode(code);
			scanRecordDO.setCreatedDate(new Date());
			scanRecordDO.setProductId(productDO.getProductId().longValue());
			scanRecordDO.setTraceFileId(traceabilityFileDO.getTraceabilityFileId().longValue());
			scanRecordDO.setScanNumber(1);
			scanRecordService.insert(scanRecordDO);
		}else{
			scanRecord.setScanNumber(scanRecord.getScanNumber()+1);
			scanRecordService.updateById(scanRecord);
		}


		return result;
	}

	@Override
	public JSONObject checkCode(String code) {
		JSONObject result = new JSONObject();
		CodeFlagDO codeFlagDO =  codeFlagService.getCodeFlagByCode(code);
		if (codeFlagDO==null){
			result.put("msg","防伪码首次查询");
			CodesDO codesBySmallCode = codesService.getCodesBySmallCode(code);
			if (codesBySmallCode!=null){
				codeFlagDO = new CodeFlagDO();
				codeFlagDO.setCode(code);
				codeFlagDO.setTime(new Date());
				codeFlagDO.setSize(1);
				codeFlagService.insert(codeFlagDO);
			}else{
				result.put("msg","防伪码不存在");
			}
		}else{
			String time = DateUtils.format(codeFlagDO.getTime());
			result.put("msg","该防伪码已经查询过，首次查询时间:"+time);
			codeFlagDO.setSize(codeFlagDO.getSize()+1);
			codeFlagService.updateById(codeFlagDO);
		}
		return result;
	}

	@Override
	public Page getPage(Long merchantId, int pageNumber, int pageSize, String searchKey) {
		Page page = new Page(pageNumber, pageSize);
		Wrapper<WxUserInfoDO> wrapper = new EntityWrapper<WxUserInfoDO>()
				.eq("merchant_id", merchantId)
				.like("nickname", searchKey);
		selectPage(page, wrapper);
		return page;
	}

	@Override
	public List<WxUserInfoVO> countProvinceUser() {
		List<WxUserInfoVO> wxUserInfoVOS = wxUserInfoDao.countProvinceUser();
		return wxUserInfoVOS;
	}

}
